package com.example.mybatisdebugtest.controller;

import com.example.mybatisdebugtest.entity.User;
import com.example.mybatisdebugtest.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    //调用业务层
    @Autowired
    UserService userService;

    //登录
    @RequestMapping("/dologin")
    @ResponseBody
    public Map doLogin(HttpSession session, String name, String password) {
        Map map = new HashMap();

        //业务层操作
        User user = userService.login(name, password);
        if (user == null) {
            map.put("isok", false);
            map.put("error", "用户名或者密码输入错误");
        } else if (user.getStatus() != 1) {
            //禁用的操作——被禁用的客户将不能进入网站即列入黑名单或者已离职【拦截器的操作】
            map.put("isok", false);
            map.put("error", "该用户已被禁用");
        } else {
            session.setAttribute("user", user);
            map.put("isok", true);
            map.put("message", "登录成功");
        }

        return map;
    }


    //注册
    @RequestMapping("/doregister")
    @ResponseBody
    public Map doRegister(String name, String password, String repassword) {
        Map map = new HashMap();
        //业务层操作
        String[] arr = isUser(name,password,repassword);

        if(arr[0].length()>0 || arr[1].length()>0 || arr[2].length()>0){
            map.put("isok", false);
            map.put("error1", arr[0]);
            map.put("error2", arr[1]);
            map.put("error3", arr[2]);
        }else {
            if (userService.register(name, password)) {
                map.put("isok", true);
                map.put("message1", "注册成功");
                map.put("message2", "添加成功");

            } else {
                map.put("isok", false);
                map.put("error4", "系统繁忙，创建账户失败");
            }
        }
        return map;
    }
    private String[] isUser(String name, String password, String repassword){
        String error1 = "",error2 = "",error3 = "",error4 = "";
        if (name == null || "".equals(name)) {
            error1 = "用户名为空";
        }
        if (isExists(name)) {
            error2 = "用户名已存在";
        }
        if (password == null || "".equals(password)) {
            error3 = "密码为空";
        }else if (!password.equals(repassword)) {
            error4 = "密码不一致";
        }
        return new String[]{error1,error2,error3,error4};
    }
    private boolean isExists(String name){
        boolean flag = false;
        List<User> list = userService.queryUserAll();
        for (User u : list) {
            if (u.getName().equals(name)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    //修改密码
    @RequestMapping("/doupdatepassword")
    @ResponseBody
    public Map doUpdatePassword(String name, String password, String repassword) {
        Map map = new HashMap();
        System.out.println(name + "-----------" + password + "---------" + repassword);
        //业务层的操作
        String[] arr = isUser(name,password,repassword);
        for (String s : arr) {
            System.out.println(s);
        }


        if(arr[0].length()>0 || arr[1].length()<=0){
            map.put("isok", false);
            map.put("error1","该用户名不存在");
        }else{
            if(arr[2].length()>0 || arr[3].length()>0){
                map.put("isok", false);
                map.put("error2",arr[2]);
                map.put("error3",arr[3]);
            } else {
                if (userService.updatePassword(name, password)) {
                    map.put("isok", true);
                    map.put("message", "密码修改成功");
                    System.out.println("=========");
                } else {
                    map.put("isok", false);
                    map.put("error4", "系统繁忙,修改失败");
                }
            }
        }
        System.out.println("====================");
        return map;
    }

    //分页+模糊查询
    @RequestMapping("/queryuserbypageorsearch")
    @ResponseBody
    public Map queryUserByPageOrSearch(
            @RequestParam(name = "pagenum", required = true, defaultValue = "1") Integer pagenum,
            @RequestParam(name = "searchname", required = true, defaultValue = "") String searchname,
            @RequestParam(name = "searchstatus", required = true, defaultValue = "") String searchstatus) {
        Map map = new HashMap();

        PageHelper.startPage(pagenum, 3);
        //业务层的操作

        List<User> list = userService.queryUserByNameOrStatusStr(searchname, searchstatus);
        PageInfo<User> pageinfo = new PageInfo<>(list);

        map.put("users", list);
        map.put("pageinfo", pageinfo);
        return map;
    }

    //修改管理员状态
    @RequestMapping("/doupdateuserstatus")
    @ResponseBody
    public Map doUpdateUserStatus(Integer id, String statusStr) {
        Map map = new HashMap();
        if (!("离职".equals(statusStr) || "禁用".equals(statusStr) || "正常".equals(statusStr))) {
            map.put("isok", false);
            map.put("error", "输入错误,修改失败");
        }

        //业务层的操作
        if (userService.updateStatus(id, statusStr)) {
            map.put("isok", true);
            map.put("message", "修改成功");
        } else {
            map.put("isok", false);
            map.put("error", "系统繁忙,修改失败");
        }

        return map;
    }


    //修改基本信息
    @RequestMapping("/doupdateser")
    @ResponseBody
    public Map doUpdateUser(Integer id, String name, String password, String personInfo) {
        Map map = new HashMap();
        boolean flag = false;

        //业务层的操作
        if (name == null || "".equals(name)
                || password == null || "".equals(password)) {
            map.put("isok", false);
            map.put("error", "用户名或者密码为空");
            return map;
        }

        User user = userService.queryUser(id);
        List<User> list = userService.queryUserAll();
        for (User u : list) {
            if (u.getName().equals(name) && !user.getName().equals(name)) {
                flag = true;
                break;
            }
        }
        if (flag) {
            map.put("isok", false);
            map.put("error", "该用户名已存在,修改失败");
            return map;
        }

        user.setName(name);
        user.setPassword(password);
        user.setPersonInfo(personInfo);
        if (userService.update(user)) {
            map.put("isok", true);
            map.put("message", "修改成功");
        } else {
            map.put("isok", false);
            map.put("error", "系统繁忙,修改失败");
        }
        return map;
    }

    //删除——直接删除
    @RequestMapping("/dodelete")
    @ResponseBody
    public Map doDelete(Integer id) {
        Map map = new HashMap();
        if (userService.deleteById(id)) {
            map.put("isok", true);
            map.put("message", "删除成功");
        } else {
            map.put("isok", false);
            map.put("error", "系统繁忙,删除失败");
        }
        return map;
    }

    //根据管理员状态删除多个的管理员
    @RequestMapping("/dodeletebystatus")
    @ResponseBody
    public Map doDeleteByStatus(String statusStr) {
        Map map = new HashMap();
        if (userService.deleteByStatus(statusStr)) {
            map.put("isok", true);
            map.put("message", "删除成功");
        } else {
            map.put("isok", false);
            map.put("error", "系统繁忙,删除失败");
        }
        return map;
    }

    // 离职的设置——离职30天后该管理员的信息将被删除
}
